package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Arrays;

//@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String source) {
        /*
        if(source.equals("1")){
            return ItemType.APARTMENT;
        }else if("2".equals(source)){
            return ItemType.ROOM;
        }else {
            return null;
        }
         */
        // ItemType.values() : 返回ItemType中的所有枚举值 ， 返回数组类型
        return Arrays.stream(ItemType.values())
                .filter(itemType -> (""+itemType.getCode()).equals(source))
                .findFirst().orElseThrow(IllegalArgumentException::new) ;
    }
}
